package com.lee.boot.order.controller;

import com.lee.boot.order.common.Result;
import com.lee.boot.order.service.ProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/product")
public class ProductController {


    @Autowired
    ProductInfoService productInfoService;

    @PostMapping("/buy/{productId}/{count}")
    @ResponseBody
    public Result buyProduct(@PathVariable("productId") String productId, @PathVariable("count") Integer count) {
//        Boolean result = productInfoService.buyProduct(productId, count);
        // 使用了不重入的乐观锁
//        Boolean result = productInfoService.buyProductOptimismLock(productId, count);
        // 使用重入乐观锁
        Boolean result = productInfoService.buyProductOptimismLockReentrant(productId, count);

        return Result.builder().code(result ? 1 : 2).message(result ? "购买成功" : "购买失败").build();
    }
    @GetMapping("/page")
    public String jumpPage() {
        return "order";
    }



}
